<template>
  <div>
    <div>
      <!--条件筛选-->
      <TopFilter :list="topFilterList" :cols="3" labelWidth="85" @filterChange="changeHandle"></TopFilter>

      <FilterTable
        ref="table"
        :columns="columns"
        :datakey="'rows'"
        :isSubmitBtn="false"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="selectChange"
        :dataSource="dataSource"
        columnsRef="xkkhnewTable"
        :isSelectColumn="false"
        selectionType="multiple"
        :isMemoryPagination="true"
        :defaultSelections="defaultSelections"
      ></FilterTable>
      <!-- :fetchapi="fetchApi"
        :params="fetchParams" -->
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">确定</el-button>
    </div>
  </div>
</template>

<script>
/**
 * 预到货单号测试账号:  5BS160533033
 */
import { dictionary } from '@/utils/dictMixin';
import TopFilter from '@/components/TopFilter';
import { confirmGoods, confirmNotice } from '@/api/part/report/threeOrderExecution';
import { confirmAction } from '@/utils';
export default {
  name: 'decorProduct',
  components: {},
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      columns: this.createTableColumns(),
      topFilterList: this.createTopFilters(),
      visibleProject: false,
      formList: [],
      fetchApi: confirmGoods,
      fetchParams: { xhrAbort: true },
      selectedRows: [],
      dataSource: [],
      defaultSelections: this.dataSource // 默认选中
    };
  },
  created() {},
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '预到货单号',
          fieldName: 'I_SVSCH',
          placeholder: '请输入...',
          rules: [{ required: true, message: '预到货单号不能为空', trigger: 'blur' }]
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '预到货单号',
          dataIndex: 'SVSCH'
        },
        {
          title: '订货申请单号',
          dataIndex: 'VBELN'
        },
        {
          title: '行项',
          dataIndex: 'POSNR'
        },
        {
          title: '配件代码',
          dataIndex: 'MATNR'
        },
        {
          title: '配件名称',
          dataIndex: 'MAKTX'
        },
        {
          title: '订货数量',
          dataIndex: 'MENGE'
        },
        {
          title: '预计送货数量',
          dataIndex: 'EN_MENGE'
        },
        {
          title: '确认到货数量',
          dataIndex: 'CONFIRM',
          editType: 'number',
          minWidth: 100,
          precision: 0,
          rules: [
            { required: true, message: '请输入数量', trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                if (value > this.selectedRows.EN_MENGE) {
                  return callback(new Error('确认到货数量不能大于预计送货数量'));
                } else {
                  callback();
                }
              },
              trigger: 'change'
            }
          ],
          editable: true,
          editRequired: true
          // onInput: (val, row) => {
          //   if (Number(val) > row.EN_MENGE) {
          //     this.$notify.warning({ title: '提示信息', message: '行项' + row.POSNR + ': 确认到货数量大于预计送货数量' });
          //   }
          // }
        }
      ];
    },

    formChangeHandle(val) {
      this.saveData = val;
    },
    // 保存按钮
    async saveDialog() {
      // if (this.selectedRows.length > 0 && this.dataSource.length > 0) {
      //   if (this.selectedRows.length != this.dataSource.length) {
      //     return this.$notify.error({ title: '提示信息', message: '请选择全部数据' });
      //   }
      // }
      // console.log(1232, this.dataSource);
      // if (this.dataSource.length < 1) {
      //   // return this.$notify.error({ title: '提示信息', message: '请选择一条数据' });
      //   return this.$notify.error({ title: '提示信息', message: '请选择一条数据' });
      // }
      // 校验必填字段
      let err = await this.$refs.table.GET_REQUIRED_ERROR();
      if (err) {
        return;
      }
      // this.visibleArrival = false;
      try {
        this.dataSource.forEach(element => {
          if (+element.CONFIRM > +element.EN_MENGE) {
            throw new Error('行项' + element.POSNR + ': 确认到货数量大于预计送货数量');
          }
        });
      } catch (Error) {
        return this.$notify.warning({ title: '提示信息', message: Error.message });
      }
      await confirmAction('该订单只能确认一次,是否继续');
      // 校验是否选中
      let params = {};
      params.list = _.cloneDeep(this.dataSource);
      params.epr3sps075ImportParameterModel = {};
      params.epr3sps075ImportParameterModel.I_SVSCH = this.topFilterList[0].initialValue;
      const res = await confirmNotice(params);
      if (res.resultCode == 200) {
        this.$emit('close', false);
        this.$notify.success({ title: '提示信息', message: res.data || '保存成功!' });
      }
    },
    searchProjectCode() {
      this.visibleProject = true;
    },
    selectChange(rows) {
      // 选中时触发的函数
      this.selectedRows = rows;
      // this.isChoice = this.selectedRows.length > 0 ? false : true;
    },
    // 搜索
    async changeHandle(val) {
      // this.fetchParams.xhrAbort = true;
      // this.fetchParams = Object.assign(this.fetchParams, { xhrAbort: false }, val);
      let res = await confirmGoods(Object.assign(this.fetchParams, { xhrAbort: false }, val));
      this.dataSource = [];
      this.dataSource = res.data;
      // this.defaultSelections = [];
      // this.defaultSelections = this.dataSource;
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>

<style lang="less" scoped></style>
